#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, dist[1001];
struct Node {
	ll y, v;
	Node (ll _y, ll _v) {
		y = _y;
		v = _v;
	}
};
vector<Node> edge[1001];
bool b[1001];
inline void Prim() {
	memset(dist, 127, sizeof(dist));
	memset(b, false, sizeof(b));
	dist[1] = 0;
	ll ans = 0;
	for (ll i = 1; i <= n; ++i) {
		ll x = -1;
		for (ll j = 1; j <= n; ++j)
			if (!b[j] && dist[j] < 1 << 30)
				if (x == -1 || dist[j] < dist[x])
					x = j;
		ans += dist[x];
		b[x] = true;
		for (auto j : edge[x])
			dist[j.y] = min(dist[j.y], j.v);	
	}
	printf("%lld\n", ans);
}

int main() {
	scanf("%lld%lld", &n, &m);
	for (ll i = 1;  i <= m; ++i) {
		ll x, y, z;
		scanf("%lld%lld%lld", &x, &y, &z);
		edge[x].push_back(Node(y, z));
		edge[y].push_back(Node(x, z));
	}
	Prim();
	return 0;
}
